import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class WizardGame extends BasicGame{
	private TiledMap map;
	private Animation sprite, up, down, left, right;
	private int duration = 300; //miliseconds
	private float wizardX, wizardY;
	private boolean[][] blockMap;
	private static final int TILE_SIZE = 32;
	
	public WizardGame( ) {
		super("Wizard Game");
	}
	public static void main(String[] args) throws SlickException {
		AppGameContainer app = new AppGameContainer(new WizardGame());
		app.setDisplayMode(800, 600, false);
		app.start();
	}
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		map.render(50, 50);
		sprite.draw(wizardX, wizardY);
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		map = new TiledMap("map/testmap.tmx");
		Image[] movementUp = {new Image("wizard/wmg1_bk1.png"), new Image("wizard/wmg1_bk1.png")};
		Image[] movementDown = {new Image("wizard/wmg1_fr1.png"),new Image("wizard/wmg1_fr2.png")};
		Image[] movementLeft = {new Image("wizard/wmg1_lf1.png"), new Image("wizard/wmg1_lf2.png")};
		Image[] movementRight = {new Image("wizard/wmg1_rt1.png"),new Image("wizard/wmg1_rt2.png")};
		up = new Animation(movementUp, duration, false);
		down = new Animation(movementDown, duration, false);
		left = new Animation(movementLeft, duration, false);
		right = new Animation(movementRight, duration, false);
		sprite = right;
		wizardX = 250f;
		wizardY = 250f;
		//System.out.println(map.getHeight());
		//System.out.println(map.getWidth());
		blockMap = new boolean[map.getHeight()*TILE_SIZE][map.getHeight()*TILE_SIZE];
		for(int x=0; x<map.getWidth(); x++) {
			for(int y=0; y<map.getHeight(); y++) {
				int tiledID = map.getTileId(x, y, 0);
				String value = map.getTileProperty(tiledID, "blocked", "false");
				System.out.println("x: "+x+"y: "+y +" "+value);
				if(value.equals("true")) blockMap[x][y] = true;
			}
		}
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Input input = gc.getInput();
		if(input.isKeyDown(Input.KEY_W)) {
			sprite = up;
			if(!isBlocked(wizardX,wizardY - delta * 0.1f)) {
				sprite.update(delta);
				wizardY -= delta * 0.1f;
			}
		}
		if(input.isKeyDown(Input.KEY_S)) {
			sprite = down;
			if(!isBlocked(wizardX, wizardY + delta*0.1f)) {
				sprite.update(delta);
				wizardY += delta * 0.1f;
			}
		}
		if(input.isKeyDown(Input.KEY_A)) {
			sprite = left;
			if(!isBlocked(wizardX - delta*0.1f, wizardY)) {
				sprite.update(delta);
				wizardX -= delta * 0.1f;
			}
		}
		if(input.isKeyDown(Input.KEY_D)) {
			sprite = right;
			if(!isBlocked(wizardX + delta*0.1f, wizardY)) {
				sprite.update(delta);
				wizardX += delta * 0.1f;
			}
		}
	}
	public boolean isBlocked(float x, float y) {
		int xBlock = (int) x/TILE_SIZE;
		int yBlock = (int) y/TILE_SIZE;
		return blockMap[xBlock][yBlock];
	}
}
